package com.xiang.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiang.reggie.common.Result;
import com.xiang.reggie.entity.Employee;
import com.xiang.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * @author 半山居雾
 * @date 2022/12/14 14:43
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param request  请求
     * @param employee 员工
     * @return {@code Result<Employee>}
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.获取明文密码
        String password = employee.getPassword();
        //md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.查询数据库用户信息
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3.判断用户信息非空
        if (emp == null) {
            return Result.error("登录失败");
        }

        //4.密码比对
        if (!emp.getPassword().equals(password)) {
            return Result.error("登录失败");
        }

        //5.查看状态
        if (emp.getStatus() == 0) {
            return Result.error("账号已禁用");
        }

        //6.登录成功，将员工id存入session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    /**
     * 注销
     *
     * @param request 请求
     * @return {@code Result<String>}
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前员登录员工的id
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");

    }

    /**
     * 添加员工
     *
     * @param request  请求
     * @param employee 员工
     * @return {@code Result<String>}
     */
    @PostMapping
    public Result<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        //设置初始密码，用md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        /* 使用元数据对象处理器，不需要手动赋值
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());



        //获取当前用户id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/



        //调用添加方法
        employeeService.save(employee);
        return Result.success("新增员工成功");
    }

    /**
     * 查询页面
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param name     名字
     * @return {@code Result<Page>}
     */
    @GetMapping("/page")
    public Result<Page> queryByPage(Integer page, Integer pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //1.构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //2.构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //3.添加过滤条件
        queryWrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);

        //4.添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //5.执行查询
        employeeService.page(pageInfo, queryWrapper);
        return Result.success(pageInfo);
    }


    /**
     * 更新
     *
     * @param request  请求
     * @param employee 员工
     * @return {@code Result<String>}
     */
    @PutMapping
    public Result<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());

        /*  已使用自动填充替换手动赋值
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/


        employeeService.updateById(employee);
        return Result.success("员工信息修改成功");

    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return {@code Result<Employee>}
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("没有查询到对应员工信息");
    }




}
